base_json_list =[
        {
            "nodeId": "20008",
            "treeId": "9",
            "nodeName": "新人教版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 8
        },
        {
            "nodeId": "20010",
            "treeId": "11",
            "nodeName": "浙教版2019新教材",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 10
        },
        {
            "nodeId": "20007",
            "treeId": "8",
            "nodeName": "新人教版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 7
        },
        {
            "nodeId": "9510",
            "treeId": "10",
            "nodeName": "G教1更1他1",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 9
        },
        {
            "nodeId": "111",
            "treeId": "12",
            "nodeName": "待编辑",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 11
        },
        {
            "nodeId": "20006",
            "treeId": "7",
            "nodeName": "新人教版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 6
        },
        {
            "nodeId": "9341",
            "treeId": "9",
            "nodeName": "treeId9-层级1-节点1",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 8
        },
        {
            "nodeId": "36127",
            "treeId": "192",
            "nodeName": "新人教版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 4
        },
        {
            "nodeId": "20011",
            "treeId": "12",
            "nodeName": "通用版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 11
        },
        {
            "nodeId": "102",
            "treeId": "3",
            "nodeName": "人教A版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 2
        },
        {
            "nodeId": "20599",
            "treeId": "10",
            "nodeName": "新人教版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 9
        },
        {
            "nodeId": "9158",
            "treeId": "6",
            "nodeName": "treeId6-层级1-节点2",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 5
        },
        {
            "nodeId": "9121",
            "treeId": "4",
            "nodeName": "treeId4-层级1-节点2",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 3
        },
        {
            "nodeId": "20005",
            "treeId": "6",
            "nodeName": "新人教版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 5
        },
        {
            "nodeId": "107",
            "treeId": "8",
            "nodeName": "人教版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 7
        },
        {
            "nodeId": "101",
            "treeId": "2",
            "nodeName": "（陈超添加）层级1-节点11",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 1
        },
        {
            "nodeId": "20003",
            "treeId": "4",
            "nodeName": "新人教版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 3
        },
        {
            "nodeId": "20002",
            "treeId": "3",
            "nodeName": "新人教A版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 2
        },
        {
            "nodeId": "110",
            "treeId": "11",
            "nodeName": "待编辑",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 10
        },
        {
            "nodeId": "106",
            "treeId": "7",
            "nodeName": "待编辑",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 6
        },
        {
            "nodeId": "20504",
            "treeId": "3",
            "nodeName": "新人教B版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 2
        },
        {
            "nodeId": "9168",
            "treeId": "7",
            "nodeName": "treeId7-层级1-节点2",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 6
        },
        {
            "nodeId": "9541",
            "treeId": "10",
            "nodeName": "教22",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 9
        },
        {
            "nodeId": "20001",
            "treeId": "2",
            "nodeName": "新人教版",
            "isShow": 1,
            "imageUrl": "https://cdn.ewt360.com/resources/school-admin/uploads/2025/5/26/1504ef3f-44bf-4eed-b86b-98bff9fe7ba1",
            "subjectId": 1
        },
        {
            "nodeId": "20563",
            "treeId": "7",
            "nodeName": "新浙科版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 6
        },
        {
            "nodeId": "9466",
            "treeId": "9",
            "nodeName": "测试超过数目9",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 8
        },
        {
            "nodeId": "20601",
            "treeId": "10",
            "nodeName": "新湘教版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 9
        },
        {
            "nodeId": "9109",
            "treeId": "3",
            "nodeName": "treeId3-层级1-节点2改编111",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 2
        },
        {
            "nodeId": "9122",
            "treeId": "4",
            "nodeName": "treeId4-层级1-节点3",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 3
        },
        {
            "nodeId": "20555",
            "treeId": "6",
            "nodeName": "新鲁科版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 5
        },
        {
            "nodeId": "9190",
            "treeId": "8",
            "nodeName": "treeId8-层级1-节点2",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 7
        },
        {
            "nodeId": "9094",
            "treeId": "2",
            "nodeName": "（西瓜添加）层级1-节点2",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 1
        },
        {
            "nodeId": "20539",
            "treeId": "4",
            "nodeName": "新外研版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 3
        },
        {
            "nodeId": "20541",
            "treeId": "4",
            "nodeName": "新北师大版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 3
        },
        {
            "nodeId": "20506",
            "treeId": "3",
            "nodeName": "新湘教版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 2
        },
        {
            "nodeId": "9169",
            "treeId": "7",
            "nodeName": "treeId7-层级1-节点3",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 6
        },
        {
            "nodeId": "10596",
            "treeId": "2",
            "nodeName": "(小峰)层级1-1",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 1
        },
        {
            "nodeId": "20565",
            "treeId": "7",
            "nodeName": "新苏教版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 6
        },
        {
            "nodeId": "9097",
            "treeId": "3",
            "nodeName": "treeId3-层级1-节点1",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 2
        },
        {
            "nodeId": "10931",
            "treeId": "9",
            "nodeName": "测试一级节点",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 8
        },
        {
            "nodeId": "20603",
            "treeId": "10",
            "nodeName": "新中图版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 9
        },
        {
            "nodeId": "20557",
            "treeId": "6",
            "nodeName": "新苏教版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 5
        },
        {
            "nodeId": "9193",
            "treeId": "8",
            "nodeName": "treeId8-层级1-节点3",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 7
        },
        {
            "nodeId": "11029",
            "treeId": "8",
            "nodeName": "小峰政治1级",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 7
        },
        {
            "nodeId": "10020",
            "treeId": "2",
            "nodeName": "（创业添加）",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 1
        },
        {
            "nodeId": "20543",
            "treeId": "4",
            "nodeName": "新译林版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 3
        },
        {
            "nodeId": "20508",
            "treeId": "3",
            "nodeName": "新苏教版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 2
        },
        {
            "nodeId": "10912",
            "treeId": "9",
            "nodeName": "一级节点新增测试",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 8
        },
        {
            "nodeId": "20605",
            "treeId": "10",
            "nodeName": "新鲁教版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 9
        },
        {
            "nodeId": "10172",
            "treeId": "2",
            "nodeName": "巴顿添加",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 1
        },
        {
            "nodeId": "20512",
            "treeId": "3",
            "nodeName": "新北师大版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 2
        },
        {
            "nodeId": "9116",
            "treeId": "3",
            "nodeName": "treeId3-层级1-节点3",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 2
        },
        {
            "nodeId": "10936",
            "treeId": "9",
            "nodeName": "小峰1级",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 8
        },
        {
            "nodeId": "11030",
            "treeId": "9",
            "nodeName": "小峰1级2",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 8
        },
        {
            "nodeId": "9491",
            "treeId": "2",
            "nodeName": "（凯利添加）层级1-节点1",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 1
        },
        {
            "nodeId": "10184",
            "treeId": "2",
            "nodeName": "测试节点",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 1
        },
        {
            "nodeId": "11151",
            "treeId": "2",
            "nodeName": "张伟伟测试2",
            "isShow": 1,
            "imageUrl": "https://test-si.ewt360.com/resources/school-admin/uploads/2024/10/31/8b79fd74-c91b-45e3-9b43-5d9cb8452853.png",
            "subjectId": 1
        },
        {
            "nodeId": "11418",
            "treeId": "2",
            "nodeName": "byt教材版本0919测试byt教材版本0919测试byt教材版本0919测试",
            "isShow": 1,
            "imageUrl": "https://test-si.ewt360.com/resources/school-admin/uploads/2024/10/31/8b79fd74-c91b-45e3-9b43-5d9cb8452853.png",
            "subjectId": 1
        },
        {
            "nodeId": "11467",
            "treeId": "2",
            "nodeName": "人教版",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 1
        },
        {
            "nodeId": "11526",
            "treeId": "2",
            "nodeName": "有教材版本无书本节点",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 1
        },
        {
            "nodeId": "11528",
            "treeId": "2",
            "nodeName": "有教材有书本无章节点",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 1
        },
        {
            "nodeId": "11534",
            "treeId": "2",
            "nodeName": "有教材有书本有章节,章节资源为空",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 1
        },
        {
            "nodeId": "11540",
            "treeId": "2",
            "nodeName": "有教材有书本有章有节,节点资源为空",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 1
        },
        {
            "nodeId": "36048",
            "treeId": "2",
            "nodeName": "教材版本是自尊会员",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 1
        },
        {
            "nodeId": "36008",
            "treeId": "2",
            "nodeName": "书本层级以下测试自尊会员",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 1
        },
        {
            "nodeId": "36010",
            "treeId": "2",
            "nodeName": "书本层级以下测试节点隐藏逻辑",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 1
        },
        {
            "nodeId": "36058",
            "treeId": "2",
            "nodeName": "教材版本测试节点隐藏逻辑",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 1
        },
        {
            "nodeId": "36070",
            "treeId": "2",
            "nodeName": "自尊会员引起的书本层级为空",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 1
        },
        {
            "nodeId": "36088",
            "treeId": "2",
            "nodeName": "隐藏节点引起的书本层级为空",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 1
        },
        {
            "nodeId": "36131",
            "treeId": "2",
            "nodeName": "空教材版本1",
            "isShow": 1,
            "imageUrl": "",
            "subjectId": 1
        }
    ]


chinese =


math =
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
english =

physics=

chemistry=

bio =

politics=
history=
geo=

import json


def find_node_by_name(node_list, node_name):
    """
    递归查找节点名称匹配的节点

    Args:
        node_list: 节点列表
        node_name: 要查找的节点名称

    Returns:
        匹配的节点对象，如果没找到返回None
    """
    for node in node_list:
        if node.get('nodeName') == node_name:
            return node

        # 递归查找子节点
        if 'subNodeList' in node and node['subNodeList']:
            found_node = find_node_by_name(node['subNodeList'], node_name)
            if found_node:
                return found_node

    return None


def generate_properties_config(base_json_list, *config_list):
    """
    根据给定的逻辑生成properties配置，支持多个配置对象匹配

    Args:
        base_json_list: 基础JSON列表
        *config_list: 多个配置对象 (config_a, config_b, config_c, ...)

    Returns:
        生成的配置列表
    """
    results = []
    global_index = 0  # 全局索引计数器

    for base_item in base_json_list:
        matched = False

        # 遍历所有配置对象，寻找匹配
        for config_index, config_obj in enumerate(config_list):
            # 检查subjectId是否匹配
            if base_item.get('subjectId') == config_obj['treeBase'].get('subjectId'):
                # 在nodeList中查找匹配的nodeName
                matched_node = find_node_by_name(config_obj['nodeList'], base_item.get('nodeName'))

                if matched_node:
                    # 生成配置项
                    config = {
                        'index': global_index,
                        'baseEditionId': base_item.get('nodeId'),
                        'nodeId': matched_node.get('id'),
                        'subjectId': base_item.get('subjectId'),
                        'sourceName': base_item.get('nodeName'),
                        'configIndex': config_index,  # 记录来自哪个配置对象
                        'treeId': config_obj['treeBase'].get('treeId')
                    }
                    results.append(config)
                    print(
                        f"匹配成功 [配置{config_index + 1}]: {base_item.get('nodeName')} (subjectId={base_item.get('subjectId')}) -> nodeId: {matched_node.get('id')}")
                    matched = True
                    global_index += 1
                    break  # 找到匹配后跳出配置循环

        if not matched:
            print(f"未找到匹配: {base_item.get('nodeName')} (subjectId={base_item.get('subjectId')})")

    return results


def format_as_properties(configs):
    """
    将配置格式化为properties文件格式

    Args:
        configs: 配置列表

    Returns:
        properties格式的字符串
    """
    # properties_content = []
    #
    # for config in configs:
    #     index = config['index']
    #     properties_content.append(f"istudy.edition-map-list[{index}].baseEditionId={config['baseEditionId']}")
    #     properties_content.append(f"istudy.edition-map-list[{index}].nodeId={config['nodeId']}")
    #     properties_content.append(f"istudy.edition-map-list[{index}].subjectId={config['subjectId']}")
    #
    # return '\n'.join(properties_content)
    """
        将配置格式化为 istudy.edition-node-map 格式
    
        Args:
            configs: 配置列表
    
        Returns:
            edition-node-map格式的字符串
        """
    # 构建映射字典
    node_map = {}
    for config in configs:
        base_edition_id = str(config['baseEditionId'])
        node_id = str(config['nodeId'])
        node_map[base_edition_id] = node_id

    # 转换为JSON字符串
    json_map = json.dumps(node_map, ensure_ascii=False, separators=(',', ':'))

    # 格式化为properties配置
    properties_line = f"istudy.edition-node-map={json_map}"

    return properties_line


def save_properties_file(content, filename='output.properties'):
    """
    保存properties内容到文件

    Args:
        content: properties内容
        filename: 输出文件名
    """
    with open(filename, 'w', encoding='utf-8') as f:
        f.write(content)
    print(f"Properties文件已保存到: {filename}")


def main():

    print("=" * 60)
    print("开始生成Properties配置 (支持多配置对象匹配)")
    print("=" * 60)

    # 生成配置 - 传入多个配置对象
    configs = generate_properties_config(base_json_list, chinese, math, english, physics, chemistry, bio, politics, history, geo)

    if configs:
        # 格式化为properties格式
        properties_output = format_as_properties(configs)

        print(f"\n生成的Properties配置 (共{len(configs)}项):")
        print("-" * 40)
        print(properties_output)

        # 保存到文件
        save_properties_file(properties_output, "D:\\pycharmProject\\pythonProject\\xinli_data_inti\\pre_output_grade_2.properties")

        print(f"\n详细匹配信息:")
        print("-" * 40)
        for config in configs:
            print(f"[{config['index']}] {config['sourceName']} (科目ID:{config['subjectId']})")
            print(f"    baseEditionId: {config['baseEditionId']}")
            print(f"    nodeId: {config['nodeId']} (来自配置{config['configIndex'] + 1})")
            print(f"    treeId: {config['treeId']}")
            print("-" * 30)

        print(f"\n统计信息:")
        print(f"总匹配项: {len(configs)}")
        print(f"总输入项: {len(base_json_list)}")
        print(f"匹配率: {len(configs) / len(base_json_list) * 100:.1f}%")
    else:
        print("未找到任何匹配的配置项!")


def generate_batch_properties(base_json_list, config_dict):
    """
    批量生成properties配置的便捷方法

    Args:
        base_json_list: 基础JSON列表
        config_dict: 配置字典，格式: {"config_name": config_obj, ...}

    Returns:
        生成的配置列表
    """
    print(f"批量处理模式: 加载了 {len(config_dict)} 个配置对象")
    config_list = list(config_dict.values())
    return generate_properties_config(base_json_list, *config_list)


def load_multiple_configs(*config_files):
    """
    加载多个配置文件

    Args:
        *config_files: 配置文件路径列表

    Returns:
        配置对象列表
    """
    configs = []
    for i, file_path in enumerate(config_files):
        try:
            with open(file_path, 'r', encoding='utf-8') as f:
                config = json.load(f)
                configs.append(config)
                print(f"成功加载配置文件 {i + 1}: {file_path}")
        except Exception as e:
            print(f"加载配置文件失败 {file_path}: {e}")

    return configs


def load_from_json_files(base_json_file, config_json_file):
    """
    从JSON文件中加载数据

    Args:
        base_json_file: base_json_list的JSON文件路径
        config_json_file: config_a的JSON文件路径

    Returns:
        tuple: (base_json_list, config_a)
    """
    try:
        with open(base_json_file, 'r', encoding='utf-8') as f:
            base_json_list = json.load(f)

        with open(config_json_file, 'r', encoding='utf-8') as f:
            config_a = json.load(f)

        return base_json_list, config_a
    except FileNotFoundError as e:
        print(f"文件未找到: {e}")
        return None, None
    except json.JSONDecodeError as e:
        print(f"JSON解析错误: {e}")
        return None, None


if __name__ == "__main__":
    # 方式1: 使用硬编码的数据 (支持多配置对象)
    main()

    print("\n" + "=" * 60)
    print("其他使用方式示例:")
    print("=" * 60)

    # 方式2: 使用配置字典的批量处理方式
    print("\n# 方式2: 批量处理示例")
    print("# config_dict = {")
    print("#     'chinese': config_a,")
    print("#     'math': config_b,")
    print("#     'english': config_c")
    print("# }")
    print("# configs = generate_batch_properties(base_json_list, config_dict)")

    # 方式3: 从多个文件加载配置
    print("\n# 方式3: 从多个文件加载")
    print("# configs_list = load_multiple_configs('config_a.json', 'config_b.json', 'config_c.json')")
    print("# if configs_list:")
    print("#     results = generate_properties_config(base_json_list, *configs_list)")
    print("#     properties_output = format_as_properties(results)")
    print("#     save_properties_file(properties_output)")

    # 方式4: 单个配置文件处理
    print("\n# 方式4: 处理单个配置")
    print("# base_json_list, single_config = load_from_json_files('base.json', 'config.json')")
    print("# if base_json_list and single_config:")
    print("#     configs = generate_properties_config(base_json_list, single_config)")
    print("#     properties_output = format_as_properties(configs)")
    print("#     save_properties_file(properties_output)")